# coding:utf-8
import sys
import time
sys.path.append("D:/test/Appium2021")
# from dos_cmd import DosCmd
from util.dos_cmd import DosCmd
from util.port import Port
import threading
from util.write_user_command import WriteUserCommand

class Server:
    def __init__(self):
        self.dos = DosCmd()
        self.device_list = self.get_devices()
        self.write_file = WriteUserCommand()
    #获取设备信息
    def get_devices(self):
        # self.dos = DosCmd()
        devices_list = []
        result_list = self.dos.excute_cmd_result('adb devices')
        if len(result_list)>=2:
            for i in result_list:
                if 'List' in i:
                    continue
                devices_info = i.split('\t')
                if devices_info[1] == 'device':
                    devices_list.append(devices_info[0])

            return devices_list

        else:
            print("获取不到设备")
            return None
    def create_port_list(self,start_port):
        port = Port()
        port_list =[]
        port_list = port.create_port_list(start_port, self.device_list)
        return port_list
    def creat_command_list(self,i):
        # appium -p 4700 -bp 4701 -U 127.0.0.1:62001
        # write_file = WriteUserCommand()
        command_list = []
        appium_port_list = self.create_port_list(4700)
        bootstrap_port_list =self.create_port_list(4900)
        device_list = self.get_devices()
        command = "appium -p " + str(appium_port_list[i]) + " -bp "+ str(bootstrap_port_list[i]) + " -U " +device_list[i] + " --no-reset --session-override --log D:/test/Appium2021/log/test" + str(i) + ".log"
        command_list.append(command)
        self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]), str(appium_port_list[i]))
        return command_list
    def start_server(self,i):
        # dos = DosCmd()
        self.start_list = self.creat_command_list(i)
        self.dos.excute_cmd(self.start_list[0])
        time.sleep(5)

    def kill_server(self):
        server_list = self.dos.excute_cmd_result('tasklist |find "node.exe"')
        if len(server_list)>0:
            self.dos.excute_cmd('taskkill -F -PID node.exe')

    def main(self):
        self.kill_server()
        self.write_file.clear_data()
        for i in range(len(self.device_list)):
            appium_start = threading.Thread(target=self.start_server, args=(i,))
            appium_start.start()
        time.sleep(25)






if __name__ == '__main__':
    server = Server()
    server.main()
    # print(server.creat_command_list())
    # print(server.start_server())
    # print(server.main())
